<html>
<head><meta charset="utf-8"><title>loop {} · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html">loop {}</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159638979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159638979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159638979">(Feb 28 2019 at 18:32)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="133224">@Nikita Popov</span></p>



<a name="159639023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639023">(Feb 28 2019 at 18:32)</a>:</h4>
<p>To anybody reading along, <a href="#narrow/stream/185694-t-compiler.2Fwg-meta/topic/LLVM.20WG/near/159637873" title="#narrow/stream/185694-t-compiler.2Fwg-meta/topic/LLVM.20WG/near/159637873">see this first</a></p>



<a name="159639201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639201">(Feb 28 2019 at 18:34)</a>:</h4>
<p><span class="user-mention" data-user-id="133224">@Nikita Popov</span> what happens is that once you have </p>
<div class="codehilite"><pre><span></span><span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">call</span> <span class="k">i32</span> <span class="vg">@foo</span><span class="p">()</span><span class="c">;</span>
<span class="k">ret</span> <span class="k">i32</span> <span class="nv nv-Anonymous">%0</span>
</pre></div>


<p>that gets optimised out in following steps:</p>
<div class="codehilite"><pre><span></span><span class="k">call</span> <span class="kt">void</span> <span class="vg">@foo</span><span class="p">()</span>
<span class="k">ret</span> <span class="k">i32</span> <span class="k">undef</span> <span class="c">; because foo never terminates</span>
</pre></div>


<p>then</p>
<div class="codehilite"><pre><span></span><span class="c">; foo call removed because it is readnone and its results are never used and it has no side-effects</span>
<span class="k">ret</span> <span class="k">i32</span> <span class="k">undef</span> <span class="c">; because foo never terminates</span>
</pre></div>



<a name="159639374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639374">(Feb 28 2019 at 18:36)</a>:</h4>
<p>At some point (does not matter whether it happens before or after the call removal) the return instruction also becomes <code>unreachable</code></p>



<a name="159639393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639393">(Feb 28 2019 at 18:36)</a>:</h4>
<p>and that is why you end with a function that contains a single <code>unreachable</code> rather than an infinite loop.</p>



<a name="159639510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639510">(Feb 28 2019 at 18:37)</a>:</h4>
<p>making sure that such <code>@foo</code> is never marked as side-effect-free (which any of <code>readnone</code>, <code>readonly</code> or <code>writeonly</code> imply) resolves this issue in my testing</p>



<a name="159639633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639633">(Feb 28 2019 at 18:39)</a>:</h4>
<p>Seems simple enough, right?</p>



<a name="159639785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639785">(Feb 28 2019 at 18:40)</a>:</h4>
<p>First thing I proposed to LLVM people is to just consider <code>noreturn</code> attribute as a "this has side-effects" but apparently it is possible that this is not always inferred right.</p>



<a name="159639916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159639916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159639916">(Feb 28 2019 at 18:41)</a>:</h4>
<p>That seems like something that should be the case regardless of whether it covers all cases...</p>



<a name="159640064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159640064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159640064">(Feb 28 2019 at 18:43)</a>:</h4>
<p>That’s how I felt as well. Then we can also somehow make sure that analysis for noreturn always runs as a dependency for the analysis for readnone and (read|write)only</p>



<a name="159640198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159640198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159640198">(Feb 28 2019 at 18:44)</a>:</h4>
<p>For that purpose you're not interested in noreturn though, but in maybe noreturn. Basically pretty much any function that has a loop without known tripcount.</p>



<a name="159640306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159640306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159640306">(Feb 28 2019 at 18:45)</a>:</h4>
<p>Indeed, and that was the part that I had trouble with <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="159640378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/loop%20%7B%7D/near/159640378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/loop.20.7B.7D.html#159640378">(Feb 28 2019 at 18:46)</a>:</h4>
<p>I’m fairly unfamiliar with how that area of code works.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>